﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Microsoft.VisualStudio.TestTools.UnitTesting;

//using Rhino.Mocks;
//using Rhino.Mocks.Interfaces;

//using Core;
//using ServerAccess;
//using SharedGenomics.Domain;

//namespace FormLogic.Tests
//{
//    [TestClass]
//    public class AnalysisResultsPresenterTests
//    {
//        #region Private Members

//        private MockRepository mocks;
//        private IAnalysisResultsView view;
//        private IAnalysisResultsModel model;
//        private TestLoginGuard loginGuard;
//        private IJobDetailsServiceProxy jobService;
//        private IApplicationController appController;
//        private IDataColumnSchemaFactory schemaFactory;
//        private AnnotationSetContextMenuProvider contextMenuProvider;

//        #endregion

//        #region Setup

//        [TestInitialize]
//        public void Setup()
//        {
//            this.mocks = new MockRepository();
//            this.loginGuard = new TestLoginGuard();

//            //set all mocks as stubs - these can be over-riden in the individual tests
//            this.view = this.mocks.Stub<IAnalysisResultsView>();
//            this.model = this.mocks.Stub<IAnalysisResultsModel>();
//            this.jobService = this.mocks.Stub<IJobDetailsServiceProxy>();
//            this.appController = this.mocks.Stub<IApplicationController>();
//            this.schemaFactory = this.mocks.Stub<IDataColumnSchemaFactory>();
//            this.contextMenuProvider = this.mocks.Stub<AnnotationSetContextMenuProvider>(null, null);
//        }

//        #endregion

//        #region Constructor Tests

//        [TestMethod]
//        public void ConstructorTest()
//        {
//            IAnalysisResultsView view = this.mocks.StrictMock<IAnalysisResultsView>();
//            IAnalysisResultsModel model = this.mocks.StrictMock<IAnalysisResultsModel>();
//            ILoginRequiredGuard loginGuard = this.mocks.Stub<ILoginRequiredGuard>();
//            IJobDetailsServiceProxy jobService = this.mocks.Stub<IJobDetailsServiceProxy>();
//            IApplicationController ac = this.mocks.Stub<IApplicationController>();
//            IDataColumnSchemaFactory schemaFactory = this.mocks.Stub<IDataColumnSchemaFactory>();
//            AnnotationSetContextMenuProvider contextMenuProvider = this.mocks.Stub<AnnotationSetContextMenuProvider>(null, null);

//            using (this.mocks.Record())
//            {
//                view.LoadRunClicked += null;
//                LastCall.IgnoreArguments();

//                view.ShowRunsClicked += null;
//                LastCall.IgnoreArguments();

//                view.ShowPathwayFrequenciesClicked += null;
//                LastCall.IgnoreArguments();

//                view.ShowUcscBrowserClicked += null;
//                LastCall.IgnoreArguments();

//                view.SelectedJobChanged += null;
//                LastCall.IgnoreArguments();

//                view.ContextMenuRequired += null;
//                LastCall.IgnoreArguments();

//                model.ValueAnnotationComplete += null;
//                LastCall.IgnoreArguments();
//            }

//            using (this.mocks.Playback())
//            {
//                AnalysisResultsPresenter presenter = new AnalysisResultsPresenter(view, model, loginGuard, jobService, ac, schemaFactory, contextMenuProvider);
//            }
//        }

//        #endregion

//        #region Event Handler Tests

//        #region ShowRuns_Clicked

//        [TestMethod]
//        public void ShowRuns_Remote_Succeeds()
//        {
//            this.model = this.mocks.DynamicMock<IAnalysisResultsModel>();

//            IEventRaiser loadClickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.ShowRunsClicked += null;
//                LastCall.IgnoreArguments();
//                loadClickedEvent = LastCall.GetEventRaiser();

//                JobDetails[] details = new[] { new JobDetails(1, JobStatus.Finished, JobType.PassocBT), new JobDetails(2, JobStatus.Running, JobType.PModel) };
//                SetupResult.For(this.jobService.LoginRequired).Return(true);
//                SetupResult.For(this.jobService.GetJobDetails()).Return(details);

//                this.model.LoadJobs(details);
//            }

//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                loadClickedEvent.Raise(null, null);

//                Assert.IsTrue(this.loginGuard.WasCalled);
//            }
//        }

//        [TestMethod]
//        public void ShowRuns_Remote_Fails_ServiceThrowsException()
//        {
//            this.view = this.mocks.DynamicMock<IAnalysisResultsView>();

//            IEventRaiser loadClickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.ShowRunsClicked += null;
//                LastCall.IgnoreArguments();
//                loadClickedEvent = LastCall.GetEventRaiser();

//                SetupResult.For(this.jobService.LoginRequired).Return(true);
//                SetupResult.For(this.jobService.GetJobDetails()).Throw(new ServiceException("!!!"));
//                this.view.DisplayError("Unable to load job details from the server: !!!");
//            }

//            using (this.mocks.Record())
//            {
//                var presenter = this.CreatePresenter();
//                loadClickedEvent.Raise(null, null);

//                Assert.IsTrue(this.loginGuard.WasCalled);
//            }
//        }

//        [TestMethod]
//        public void ShowRunsTest_Local_NoGuard()
//        {
//            this.view = this.mocks.DynamicMock<IAnalysisResultsView>();

//            IEventRaiser loadClickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.ShowRunsClicked += null;
//                LastCall.IgnoreArguments();
//                loadClickedEvent = LastCall.GetEventRaiser();

//                SetupResult.For(this.jobService.LoginRequired).Return(false);
//                SetupResult.For(this.jobService.GetJobDetails()).Return(new JobDetails[] { });
//            }

//            using (this.mocks.Record())
//            {
//                var presenter = this.CreatePresenter();
//                loadClickedEvent.Raise(null, null);

//                //guard should not be called if no login is required
//                Assert.IsFalse(this.loginGuard.WasCalled);
//            }
//        }

//        #endregion

//        #region SelectedJobChanged

//        [TestMethod]
//        public void SelectedJobChangedTest_NotNull_JobFinished()
//        {
//            this.view = this.mocks.DynamicMock<IAnalysisResultsView>();
//            IEventRaiser selectedJobChangedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.SelectedJobChanged += null;
//                LastCall.IgnoreArguments();
//                selectedJobChangedEvent = LastCall.GetEventRaiser();

//                Expect.Call(this.view.SelectedJob).Return(new JobDetails(1, JobStatus.Finished, JobType.PassocBT));

//                //set expectation on view - load button should be enabled
//                this.view.IsLoadRunEnabled = true;
//            }

//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                selectedJobChangedEvent.Raise(null, null);
//            }
//        }

//        [TestMethod]
//        public void SelectedJobChangedTest_NotNull_JobNotFinished()
//        {
//            this.view = this.mocks.DynamicMock<IAnalysisResultsView>();
//            IEventRaiser selectedJobChangedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.SelectedJobChanged += null;
//                LastCall.IgnoreArguments();
//                selectedJobChangedEvent = LastCall.GetEventRaiser();

//                Expect.Call(this.view.SelectedJob).Return(new JobDetails(1, JobStatus.Running, JobType.PassocBT));

//                //set expectation on view - load button should be disabled as the selected job has not completed
//                this.view.IsLoadRunEnabled = false;
//            }

//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                selectedJobChangedEvent.Raise(null, null);
//            }
//        }

//        [TestMethod]
//        public void SelectedJobChangedTest_Null()
//        {
//            this.view = this.mocks.DynamicMock<IAnalysisResultsView>();
//            IEventRaiser selectedJobChangedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.SelectedJobChanged += null;
//                LastCall.IgnoreArguments();
//                selectedJobChangedEvent = LastCall.GetEventRaiser();

//                Expect.Call(this.view.SelectedJob).Return(null);

//                //set expectation on view - load button should be disabled as no job is selected
//                this.view.IsLoadRunEnabled = false;
//            }

//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                selectedJobChangedEvent.Raise(null, null);
//            }
//        }

//        #endregion

//        #region LoadRunClicked

//        [TestMethod]
//        public void LoadRunClicked_LoadFromModel()
//        {
//            this.model = this.mocks.DynamicMock<IAnalysisResultsModel>();
//            IEventRaiser clickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.LoadRunClicked += null;
//                clickedEvent = LastCall.IgnoreArguments().GetEventRaiser();

//                JobDetails selectedJob = new JobDetails(1, JobStatus.Finished, JobType.PModel);
//                SetupResult.For(view.SelectedJob).Return(selectedJob);

//                this.model.LoadJob(selectedJob);
//            }

//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                clickedEvent.Raise(null, null);
//            }
//        }

//        [TestMethod]
//        public void LoadRunClicked_LoadSuccessful()
//        {
//            this.view = this.mocks.DynamicMock<IAnalysisResultsView>();
//            IAnalysisResultSet resultSet = this.mocks.Stub<IAnalysisResultSet>();
//            IEventRaiser loadClickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.LoadRunClicked += null;
//                loadClickedEvent = LastCall.IgnoreArguments().GetEventRaiser();

//                SetupResult.For(this.model.Results).Return(resultSet);
//                SetupResult.For(resultSet.ResultType).Return(typeof(int));  //NOTE: type is not important for test...

//                //create column schema returned by factory
//                TypeDataColumnSchema schema = new TypeDataColumnSchema();
//                schema.Add(new DataColumnInfo("headerText", "prop1", typeof(int)));
//                schema.Add(new DataColumnInfo("headerText", "prop2", typeof(int)));

//                SetupResult.For(this.schemaFactory.GetColumnSchemaForType(typeof(int))).Return(schema);

//                //setup expectations on view - columns should be cleared, new columns added and results should be set
//                view.ClearColumns();
//                view.AddColumn(null, null, null);
//                LastCall.IgnoreArguments().Repeat.Twice();

//                view.Results = resultSet;
//            }
//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                loadClickedEvent.Raise(null, null);
//            }
//        }

//        [TestMethod]
//        public void LoadRunClicked_LoadThrowsException()
//        {
//            this.view = this.mocks.DynamicMock<IAnalysisResultsView>();
//            IEventRaiser loadClickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.LoadRunClicked += null;
//                loadClickedEvent = LastCall.IgnoreArguments().GetEventRaiser();

//                JobDetails selectedJob = new JobDetails(1, JobStatus.Finished, JobType.PModel);
//                Expect.Call(this.view.SelectedJob).Return(selectedJob);

//                this.model.LoadJob(selectedJob);
//                LastCall.Throw(new Exception("Error opening file"));

//                this.view.DisplayError("Unable to load results for job: Error opening file");
//            }
//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                loadClickedEvent.Raise(null, null);
//            }
//        }

//        #endregion

//        #region ShowUcscBrowserClicked

//        [TestMethod]
//        public void ShowUcscBrowserClickedTest()
//        {
//            this.appController = this.mocks.StrictMock<IApplicationController>();
//            IEventRaiser clickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.ShowUcscBrowserClicked += null;
//                clickedEvent = LastCall.IgnoreArguments().GetEventRaiser();

//                int[] indices = new[] { 1, 2, 3 };
//                IList<WiggleElement> elements = new List<WiggleElement>
//                {
//                    new WiggleElement(1, 1, 2, 0.1),
//                    new WiggleElement(1, 10, 11, 0.1)
//                };

//                SetupResult.For(this.view.SelectedResultIndices).Return(indices);
//                SetupResult.For(this.model.GetSortedWiggleElementListForResults(indices)).Return(elements);

//                this.appController.CreateAndShowPresenter<UcscBrowserPresenter>(FormDisplayMode.NonModalDialog, p => p.WiggleElements = elements);
//                LastCall.IgnoreArguments();     //ignore arguments since equality is not defined for Action
//            }

//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                clickedEvent.Raise(null, null);
//            }
//        }

//        #endregion

//        #region ShowPathwayFrequenciesClicked

//        [TestMethod]
//        public void ShowPathwayFrequenciesClickedTest()
//        {
//            this.appController = this.mocks.StrictMock<IApplicationController>();
//            IEventRaiser clickedEvent;

//            using (this.mocks.Record())
//            {
//                this.view.ShowPathwayFrequenciesClicked += null;
//                clickedEvent = LastCall.IgnoreArguments().GetEventRaiser();

//                int[] indices = new[] { 1, 2, 3 };
//                var pathwayFrequencies = new List<KeggPathwayFrequency>
//                {
//                    new KeggPathwayFrequency("pw1", 2),
//                    new KeggPathwayFrequency("pw2", 5),
//                    new KeggPathwayFrequency("pw3", 1)
//                };

//                SetupResult.For(this.view.SelectedResultIndices).Return(indices);
//                SetupResult.For(this.model.GetPathwayFrequenciesForResults(indices)).Return(pathwayFrequencies);

//                this.appController.CreateAndShowPresenter<KeggPathwayFrequenciesPresenter>(FormDisplayMode.NonModalDialog, p => p.PathwayFrequencies = pathwayFrequencies);
//                LastCall.IgnoreArguments();
//            }

//            using (this.mocks.Playback())
//            {
//                var presenter = this.CreatePresenter();
//                clickedEvent.Raise(null, null);
//            }
//        }

//        #endregion

//        #endregion

//        #region Private Methods

//        private AnalysisResultsPresenter CreatePresenter()
//        {
//            return new AnalysisResultsPresenter(this.view, this.model, this.loginGuard, this.jobService, this.appController, this.schemaFactory, this.contextMenuProvider);
//        }

//        #endregion
//    }
//}
